﻿using Activity.Comm;
using Activity.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace Activity.API
{
    public static class BCOpenShopLotteryHandler
    {
        /// <summary>
        /// 结束时间
        /// </summary>
        static readonly DateTime endDate = new DateTime(2017, 01, 21, 23, 59, 59);

        /// <summary>
        /// 开始时间
        /// </summary>
        static readonly DateTime beginDate = new DateTime(2017, 01, 14, 0, 0, 0);

        /// <summary>
        /// 测试
        /// </summary>
        static readonly string isDebug = WebConfigurationManager.AppSettings["isDebug"];

        /// <summary>
        /// 检查用户抽奖情况
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static int CheckUser(string openId)
        {
            var hk = new HKEntities();
            //未关注
            //if (!hk.KM_V_CardBinding.Where(t => t.OpenID == openId).Any())
            //    return (int)ReslutType.NoRegist;
            //已关注，未抽奖
            var rpt =new ActivityEntities();
            //OrderType ==1 本次抽奖数据
            var qurey = rpt.Bas_WxOrders.Where(t => t.openid == openId && t.OrderType ==1);
            if (!qurey.Any())
                return (int)ReslutType.NoLottery;
            //已关注，已抽奖，返回抽中/未抽中
            var pId = qurey.FirstOrDefault().pid;
            if (pId == 0)
                return (int)ReslutType.NoWinning;
            else
                return (int)ReslutType.Winning;
        }
        
        /// <summary>
        /// 抽奖
        /// </summary>
        /// <returns></returns>
        public static int Lottery(string openId)
        {
            var result = 0;
            if (DateTime.Now < beginDate && isDebug.ToLower() == "false")
            {
                result = (int)ReslutType.NoStart;
                return result;
            }
            if (DateTime.Now >= endDate && isDebug.ToLower() == "false")
            {
                result = (int)ReslutType.IsStop;
                return result;
            }
            var bDate = new DateTime(2017, 01, 14, 16, 0, 0);
            var eDate = new DateTime(2017, 01, 14, 19, 0, 0);

            if (DateTime.Now < bDate && isDebug.ToLower() == "false")
            {
                result = (int)ReslutType.NoWinning;
                return result;
            }
            if (DateTime.Now >= eDate && isDebug.ToLower() == "false")
            {
                result = (int)ReslutType.NoWinning;
                return result;
            }

            var rpt = new ActivityEntities();
            //总中奖人数
            var MaxCount = 5;
            //当前中奖人数
            var lotteryCount = rpt.Bas_WxOrders.Count(t => t.OrderType == 1);
            var currCount = rpt.Bas_WxOrders.Count(t => t.pid == 4 && t.OrderType == 1);
            Log.WriteLog("总数："+ lotteryCount.ToString()+",中奖数："+currCount.ToString());
            if (currCount >= MaxCount)
            {
                //直接返回未中奖
                result = (int)ReslutType.NoWinning;
            }
            else
            {
                //5次中奖一个
                if (lotteryCount % 10 == 0 && lotteryCount != 0)
                {
                    result = (int)ReslutType.Product;
                    Log.WriteLog("抽中奖品");
                }
            }
            //插入日志
            var log = new Bas_WxOrders
            {
                openid = openId,
                createdate = DateTime.Now,
                OrderType = 1,
                pid = result
            };
            try
            {
                rpt.Bas_WxOrders.Add(log);
                rpt.SaveChanges();
            }catch(Exception ex)
            {
                Log.Error("保存日志出错，"+ex);
            }
            return result;
        }
        
        /// <summary>
        /// 获取用户地址
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static AddressData GetAddress(string openId)
        {
            var rpt = new ActivityEntities();
            var qurey = rpt.Bas_WxOrders.Where(t => t.openid == openId
            && t.OrderType == 1).FirstOrDefault();
            var result = new AddressData
            {
                UserName = qurey.username,
                Mobile = qurey.phone,
                Address = qurey.address
            };
            return result;
        }

        /// <summary>
        /// 新增/修改用户地址
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int SaveOrder(AddressData data)
        {
            var result = -1;
            var rpt = new ActivityEntities();
            //OrderType ==1 本次抽奖数据
            var qurey = rpt.Bas_WxOrders.Where(t => t.openid == data.openId && t.OrderType == 1);
            if (!qurey.Any())
            {
                var order = new Bas_WxOrders {
                    openid = data.openId,
                    pid = (int)ReslutType.Product,
                    username = data.UserName,
                    phone = data.Mobile,
                    address = data.Address,
                    createdate = DateTime.Now,
                    OrderType = 1
                };
                rpt.Bas_WxOrders.Add(order);
            }
            else
            {
                var order = qurey.FirstOrDefault();
                order.username = data.UserName;
                order.phone = data.Mobile;
                order.address = data.Address;
            }
            try
            {
                rpt.SaveChanges();
                result = 0;
            }catch(Exception ex)
            {
                Log.Error("地址保存错误，错误信息：" + ex);
                result = -1;
            }
            return result;
        }
    }
}
